package cc.wsyw126.java.collection.list;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by junpeng.yang on 16/8/28.
 */
public class TestCopyOnWriteArrayListVector {
    public static void main(String[] args) {
        List list = addCopyOnWriteArrayList();
        iteratorThroughFor2(list);

        List list1 = addVector();
        iteratorThroughFor2(list1);
    }
    public static List<Integer> addCopyOnWriteArrayList(){
        Long start = System.currentTimeMillis();
        CopyOnWriteArrayList<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList();
        for (int i = 0; i < 100000; i++) {
            copyOnWriteArrayList.add(i);
        }
        System.out.println("TestCopyOnWriteArrayListVector.addCopyOnWriteArrayList :" + (System.currentTimeMillis() - start));
        return copyOnWriteArrayList;
    }


    public static List<Integer> addVector(){
        Long start = System.currentTimeMillis();
        Vector<Integer> vector = new Vector();
        for (int i = 0; i < 100000; i++) {
            vector.add(i);
        }
        System.out.println("TestCopyOnWriteArrayListVector.addVector :" + (System.currentTimeMillis() - start));
        return vector;
    }

    public static void iteratorThroughFor2(List list) {

        long startTime;
        long endTime;
        startTime = System.currentTimeMillis();
        for(Object obj:list) ;
        endTime = System.currentTimeMillis();
        long interval = endTime - startTime;
        System.out.println("iteratorThroughFor2：" + interval+" ms");
    }
}
